/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GraphSelector.java
 *
 * Created on 26.09.2011, 15:39:23
 */
package com.transas.zorlog.ui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author amalikov
 */
@SuppressWarnings("unchecked")
class GraphSelector extends javax.swing.JPanel {

	/** Creates new form GraphSelector */
	public GraphSelector() {
		initComponents();
		
		availableList.setModel(new DefaultListModel());
		selectedList.setModel(new DefaultListModel());		
	}

	public void setAvailable(List<String> names) {
		DefaultListModel model = (DefaultListModel) availableList.getModel();
		for (String name : names)
			model.addElement(name);
	}

	public void setSelected(List<String> names) {
		List<Integer> selectedIndices = new ArrayList<>();
		DefaultListModel model = (DefaultListModel) availableList.getModel();
		for (String name : names) {
			final int index = model.indexOf(name);
			if (index >= 0)
				selectedIndices.add(index);
		}
		
		final int[] indices = new int[selectedIndices.size()];
		int i = 0;
		for (Integer value : selectedIndices)
			indices[i++] = value;
		
		availableList.setSelectedIndices(indices);
		moveSelectedElements(availableList, selectedList);
	}
	
	public List<String> getSelected() {
		List<String> res = new ArrayList<>();
		DefaultListModel model = (DefaultListModel) selectedList.getModel();
		for (Object o : model.toArray())
			res.add((String) o);
		return res;
	}
	
	private void moveSelectedElements(JList fromList, JList toList) {
		DefaultListModel from = (DefaultListModel) fromList.getModel();
		DefaultListModel to = (DefaultListModel) toList.getModel();
		
		for (Object o : fromList.getSelectedValuesList()) {
			from.removeElement(o);
			to.addElement(o);
		}
	}	
	
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        availableList = new javax.swing.JList();
        selectButton = new javax.swing.JButton();
        deselectButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        selectedList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();

        jLabel1.setText("Available");

        jScrollPane1.setViewportView(availableList);

        selectButton.setText(">>");
        selectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectButtonActionPerformed(evt);
            }
        });

        deselectButton.setText("<<");
        deselectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deselectButtonActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(selectedList);

        jLabel2.setText("Selected");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(deselectButton, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(selectButton, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(selectButton)
                        .addGap(48, 48, 48)
                        .addComponent(deselectButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

	private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed
		moveSelectedElements(availableList, selectedList);
	}//GEN-LAST:event_selectButtonActionPerformed

	private void deselectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deselectButtonActionPerformed
		moveSelectedElements(selectedList, availableList);
	}//GEN-LAST:event_deselectButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList availableList;
    private javax.swing.JButton deselectButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton selectButton;
    private javax.swing.JList selectedList;
    // End of variables declaration//GEN-END:variables
}
